package cn.pconline.officialclub.web.bbs;

import cn.pconline.autoclub.util.DateUtils;
import cn.pconline.autoclub.util.Result;
import cn.pconline.autoclub.util.WebUtils;
import cn.pconline.officialclub.entity.OActivity;
import cn.pconline.officialclub.entity.OClub;
import cn.pconline.officialclub.service.OActivityService;
import cn.pconline.officialclub.service.OClubService;
import cn.pconline.officialclub.service.OStatService;
import cn.pconline.officialclub.web.WebKit;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author xulin
 */
@RequestMapping(value = "/brand/bbs")
@Controller
public class BbsOClubController extends WebKit {

    @Autowired
    private OClubService oClubService;
    @Autowired
    private OActivityService oActivityService;
    @Autowired
    private OStatService oStatService;

    /**
     * 根据论坛版块ID查询品牌车友会(论坛列表页接口)
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/1.0/listOfficialClubByForumId.do", method = RequestMethod.GET)
    public void listOfficialClubByForumId(HttpServletRequest request, HttpServletResponse response) {
        WebUtils.setCacheHeader(response, 3600);
        WebUtils.setEncoding(request, response);
        long forumId = WebUtils.paramLong(request, "forumId", 0);
        int xssEncode = WebUtils.paramInt(request, XSS_ENCODE, XSSENCODECODE);
        String callback = WebUtils.param(request, "callback", "");
        boolean isXss = xssEncode==XSSENCODECODE;
        if(forumId == 0){
            printMsg(response, Result.dataError("缺少版块ID"));
            return;
        }
        OClub oClub = oClubService.queryClubByForumId(forumId);
        JSONObject c = new JSONObject();
       if(oClub != null){
           OActivity oActivity = oActivityService.getLatestActivity(oClub.getoClubId());
           c = oClub.getJSON(isXss);
           if(oActivity!=null && oActivity.getEndDate().after(DateUtils.getNow())){
                 JSONObject o = oActivity.getJSONFilterUbb(isXss);
                 o.put("url", systemConfig.getBrandClubRoot()+"/"+oClub.getUrl()+"/activity/a"+oActivity.getActivityId()+"/");
                 o.put("statusCode", oActivityService.getActivityStatusDesc(0, oActivity).getString("activityCode"));
                 o.put("statusMsg", oActivityService.getActivityStatusDesc(0, oActivity).getString("activityMsg"));
                 c.put("activity", o);
           }
       }
        printMsg(response, Result.success(c),callback);
    }

    /**
     * 根据论坛版块ID查询品牌车友会同时查询PV量(论坛列表页接口)
     *
     * @param request
     * @param response
     */
    @RequestMapping(value = "/1.0/listOfficialClubByForumIdV2.do", method = RequestMethod.GET)
    public void listOfficialClubByForumIdV2(HttpServletRequest request, HttpServletResponse response) {
        //设置缓存
        WebUtils.setCacheHeader(response, 3600);
        WebUtils.setEncoding(request, response);
        long forumId = WebUtils.paramLong(request, "forumId", 0);
        int xssEncode = WebUtils.paramInt(request, XSS_ENCODE, XSSENCODECODE);
        String callback = WebUtils.param(request, "callback", "");
        boolean isXss = xssEncode==XSSENCODECODE;
        if(forumId == 0){
            printMsg(response, Result.dataError("缺少版块ID"));
            return;
        }
        OClub oClub = oClubService.queryClubByForumId(forumId);
        JSONObject obj = new JSONObject();
       if(oClub != null){
           OActivity oActivity = oActivityService.getLatestActivity(oClub.getoClubId());
           obj = oClub.getJSON(isXss);
           if(oActivity!=null && oActivity.getEndDate().after(DateUtils.getNow())){
                 JSONObject o = oActivity.getJSONFilterUbb(isXss);
                 o.put("url", systemConfig.getBrandClubRoot()+"/"+oClub.getUrl()+"/activity/a"+oActivity.getActivityId()+"/");
                 o.put("statusCode", oActivityService.getActivityStatusDesc(0, oActivity).getString("activityCode"));
                 o.put("statusMsg", oActivityService.getActivityStatusDesc(0, oActivity).getString("activityMsg"));
                obj.put("activity", o);
           }
           int pv = oStatService.countPvByClubId(oClub.getoClubId());
           obj.put("pv", pv);
       }
        printMsg(response, Result.success(obj),callback);
    }
}
